<<<<<<< HEAD
from . import admin_bp 
from flask import request,jsonify
from app.models import LoginLog,OperationLog,ModuleType,OperationType,User
=======
from . import admin_bp
from flask import request, jsonify
from app.models import LoginLog, OperationLog, ModuleType, OperationType, User
>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
from app.extensions import db
from app.decorator import permission_required


<<<<<<< HEAD
@admin_bp.route('/admin/login_log/list',methods=['GET'])
@permission_required(ModuleType.ADMIN_LOG, OperationType.QUERY)
def login_list():
    page = request.args.get('page',1,type=int)
    page_size = request.args.get('page_size',10,type=int)
    search = request.args.get('searchKey','',type=str)
    start_time = request.args.get('start_time','',type=str)
    end_time = request.args.get('end_time','',type=str)
    uid = request.args.get('uid',type=int)
    
    start = start_time + ' 00:00:00'
    end = end_time + ' 23:59:59'
    
=======
@admin_bp.route('/admin/login_log/list', methods=['GET'])
@permission_required(ModuleType.ADMIN_LOG, OperationType.QUERY)
def login_list():
    page = request.args.get('page', 1, type=int)
    page_size = request.args.get('page_size', 10, type=int)
    search = request.args.get('searchKey', '', type=str)
    start_time = request.args.get('start_time', '', type=str)
    end_time = request.args.get('end_time', '', type=str)
    uid = request.args.get('uid', type=int)

    start = start_time + ' 00:00:00'
    end = end_time + ' 23:59:59'

>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
    query = LoginLog.query
    if search:
        try:
            uid_user = User.query.get(uid)
<<<<<<< HEAD
            operation_log = OperationLog(user_id=uid,email=uid_user.email,module=ModuleType.ADMIN_LOG,operation=OperationType.QUERY,detail=f'搜索 {search} 的登录日志')
=======
            operation_log = OperationLog(user_id=uid, email=uid_user.email, module=ModuleType.ADMIN_LOG,
                                         operation=OperationType.QUERY, detail=f'搜索 {search} 的登录日志')
>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
            db.session.add(operation_log)
            db.session.commit()
        except Exception as e:
            print(e)
        query = query.filter(LoginLog.email.like(f'%{search}%'))
    if start_time:
        query = query.filter(LoginLog.create_time >= start)
    if end_time:
        query = query.filter(LoginLog.create_time <= end)
<<<<<<< HEAD
    
=======

>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
    pagination = query.paginate(
        page=page,
        per_page=page_size,
        error_out=False
    )
<<<<<<< HEAD
    
    logs = [log.to_json() for log in pagination.items]
    total = pagination.total
    
=======

    logs = [log.to_json() for log in pagination.items]
    total = pagination.total

>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
    data = {
        'total': total,
        'logs': logs
    }
<<<<<<< HEAD
    
    return jsonify({'message':'获取登录日志列表成功','errcode':'0','data':data}),200


@admin_bp.route('/admin/operation_log/list',methods=['GET'])
@permission_required(ModuleType.ADMIN_LOG, OperationType.QUERY)
def operation_log_export():
    page = request.args.get('page',1,type=int)
    page_size = request.args.get('page_size',10,type=int)
    search = request.args.get('searchKey','',type=str)
    start_time = request.args.get('start_time','',type=str)
    end_time = request.args.get('end_time','',type=str)
    uid = request.args.get('uid',type=int)
    
    start = start_time + ' 00:00:00'
    end = end_time + ' 23:59:59'
    
=======

    return jsonify({'message': '获取登录日志列表成功', 'errcode': '0', 'data': data}), 200


@admin_bp.route('/admin/operation_log/list', methods=['GET'])
@permission_required(ModuleType.ADMIN_LOG, OperationType.QUERY)
def operation_log_export():
    page = request.args.get('page', 1, type=int)
    page_size = request.args.get('page_size', 10, type=int)
    search = request.args.get('searchKey', '', type=str)
    start_time = request.args.get('start_time', '', type=str)
    end_time = request.args.get('end_time', '', type=str)
    uid = request.args.get('uid', type=int)

    start = start_time + ' 00:00:00'
    end = end_time + ' 23:59:59'

>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
    query = OperationLog.query
    if search:
        try:
            uid_user = User.query.get(uid)
<<<<<<< HEAD
            operation_log = OperationLog(user_id=uid,email=uid_user.email,module=ModuleType.ADMIN_LOG,operation=OperationType.QUERY,detail=f'搜索 {search} 的操作日志')
=======
            operation_log = OperationLog(user_id=uid, email=uid_user.email, module=ModuleType.ADMIN_LOG,
                                         operation=OperationType.QUERY, detail=f'搜索 {search} 的操作日志')
>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
            db.session.add(operation_log)
            db.session.commit()
        except Exception as e:
            print(e)
        query = query.join(User).filter(User.email.like(f'%{search}%'))
    if start_time:
        query = query.filter(OperationLog.create_time >= start)
    if end_time:
        query = query.filter(OperationLog.create_time <= end)
<<<<<<< HEAD
        
=======

>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
    pagination = query.paginate(
        page=page,
        per_page=page_size,
        error_out=False
    )
<<<<<<< HEAD
    
    logs = [log.to_json() for log in pagination.items]
    total = pagination.total
    
=======

    logs = [log.to_json() for log in pagination.items]
    total = pagination.total

>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
    data = {
        'total': total,
        'logs': logs
    }
<<<<<<< HEAD
    
    return jsonify({'message':'获取操作日志列表成功','errcode':'0','data':data}),200

=======

    return jsonify({'message': '获取操作日志列表成功', 'errcode': '0', 'data': data}), 200
>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
